package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.group.AmendmentBlock;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.torefactor.AmendmentBlockUtil;
import org.akomantoso.core.visitor.AKVisitable;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * 
 * <pre>
 * 	<xsd:element name="amendmentBody">
 * 		<xsd:complexType>
 * 			<xsd:sequence minOccurs="1" maxOccurs="unbounded">
 * 				<xsd:group ref="amendmentBlock" />
 * 			</xsd:sequence>
 * 			<xsd:attributeGroup ref="coreopt" />
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public final class AmendmentBody extends CoreOptImpl implements Externalizable, AKVisitable{
	
	public static final String ELEMENT = "amendmentBody";

	// Mandatory (min 1)
	private final List<AmendmentBlock> _blocks = new ArrayList<AmendmentBlock>();

	public List<AmendmentBlock> getBlocks() {
		return _blocks;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		super.writeExternal(out);
		for (int i = 0, n = _blocks.size(); i < n; i++) {
			_blocks.get(i).writeExternal(out);
		}
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		AmendmentBlockUtil.read(in, _blocks);
	}
	
	/**
	 * @see ec.ep.visitor.AKVisitable#accept(ec.ep.visitor.AKVisitor)
	 */
	public void accept(AKVisitor visitor) {
		if (visitor.visitBegin(this)) {
			for (int i = 0, n = _blocks.size(); i < n; i++) {
				_blocks.get(i).accept(visitor);
			}
			visitor.visitEnd(this);	
		}
	}
}